#! /bin/python

################################################################################
#	\file   DebSConscript
#	\author Gregory Diamos <gregory.diamos@gatech.edu>
#   \date   Thursday March 31, 2011
#	\brief  The SCons slave build script for creating a .deb for Ocelot
################################################################################

import os, shutil, sys
Import('env') # exported by parent SConstruct

# Here's the core info for the package

DEBPATH = 'ocelot_deb'
DEBNAME = 'ocelot'
DEBVERSION = env['VERSION']
DEBMAINT = "Gregory Diamos <gregory.diamos@gatech.edu>"
DEBARCH = env['deb_arch']
DEBDEPENDS = "libboost-all-dev, llvm, llvm-dev" \
	", freeglut3-dev, libglew1.5-dev, libc6"
DEBDESC = "Ocelot is a dynamic compilation framework for PTX."
DEBDDESC = """ Ocelot is a compiler framework for PTX that includes several
 backend code generators for NVIDIA and AMD GPUs as well as x86 CPUs."""
DEBWEB = "http://code.google.com/p/gpuocelot/"
DEBLICENSE = "BSD"
DEBCOPYRIGHT = """ Copyright 2011
 GEORGIA TECH RESEARCH CORPORATION
 ALL RIGHTS RESERVED
 .
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are
 met:
    * Redistributions of source code must retain the above copyright
 notice, 	this list of conditions and the following disclaimers.
    * Redistributions in binary form must reproduce the above copyright
 notice, this list of conditions and the following disclaimers in the
      documentation and/or other materials provided with the
 distribution.
    * Neither the name of GEORGIA TECH RESEARCH CORPORATION nor the
 names of 	its contributors may be used to endorse or promote
 products derived 	from this software without specific prior
 written permission.
 .
 THIS SOFTWARE IS PROVIDED BY GEORGIA TECH RESEARCH CORPORATION ''AS IS''
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GEORGIA TECH RESEARCH
 CORPORATION BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 .
 You agree that the Software will not be shipped, transferred, exported,
 or re-exported directly into any country prohibited by the United States
 Export Administration Act and the regulations thereunder nor will be
 used for any purpose prohibited by the Act.
"""

DEBFILES = []

for installedFile in env['installed_files']:
	installedFilePath = installedFile[0]
	DEBFILES.append(("usr/" + str(installedFilePath)[11:], \
		str(installedFilePath)))
	
# Now we specify the files to be included in the .deb
# Where they should go, and where they should be copied from.
# If you have a lot of files, you may wish to generate this 
# list in some other way.
#("usr/bin/myutility",			 "#src/myutility/myutility"),
#("etc/myutility/myutility.conf",  "#misc/myutility.conf"),
	
# This is the debian package we're going to create
debpkg = '#%s_%s_%s.deb' % (DEBNAME, DEBVERSION, DEBARCH)

# and we want it to be built when we build 'debian'
env.Alias("debian", debpkg)

DEBCONTROLFILE = os.path.join(DEBPATH, "DEBIAN/control")
DEBCOPYRIGHTFILE = os.path.join(DEBPATH, "usr/share/doc/ocelot/copyright")

# This copies the necessary files into place into place.
# Fortunately, SCons creates the necessary directories for us.
for f in DEBFILES:
	# We put things in a directory named after the package
	dest = os.path.join(DEBPATH, f[0])
	# The .deb package will depend on this file
	env.Depends(debpkg, dest)
	# Copy from the the source tree.
	env.Command(dest, f[1], Copy('$TARGET','$SOURCE'))
	# The control file also depends on each source because we'd like
	# to know the total installed size of the package
	env.Depends(DEBCONTROLFILE, dest)

# Now to create the control file:

CONTROL_TEMPLATE = """
Package: %s
Priority: extra
Section: misc
Installed-Size: %s
Maintainer: %s
Architecture: %s
Version: %s
Depends: %s
Description: %s
%s
"""
env.Depends(debpkg, DEBCONTROLFILE)

# The control file should be updated when the SVN version changes
env.Depends(DEBCONTROLFILE, env.Value(DEBVERSION))

# This function creates the control file from the template and info
# specified above, and works out the final size of the package.
def make_control(target=None, source=None, env=None):
	installed_size = 0
	for i in DEBFILES:
		installed_size += os.stat(str(env.File(i[1])))[6]
	installed_size = int(installed_size / 1024)
	control_info = CONTROL_TEMPLATE % (
		DEBNAME, installed_size, DEBMAINT, DEBARCH,
		DEBVERSION, DEBDEPENDS, DEBDESC, DEBDDESC)
	f = open(str(target[0]), 'w')
	f.write(control_info)
	f.close()
	
# We can generate the control file by calling make_control
env.Command(DEBCONTROLFILE, None, make_control)

COPYRIGHT_TEMPLATE = """
Name: %s
Maintainer: %s
Source: %s
License: %s
%s
"""

env.Depends(debpkg, DEBCOPYRIGHTFILE)

# This function creates the control file from the template and info
# specified above, and works out the final size of the package.
def make_copyright(target=None, source=None, env=None):
	copyright_info = COPYRIGHT_TEMPLATE % (
		DEBNAME, DEBMAINT, DEBWEB,
		DEBLICENSE, DEBCOPYRIGHT)
	f = open(str(target[0]), 'w')
	f.write(copyright_info)
	f.close()

# create the copyright file
env.Command(DEBCOPYRIGHTFILE, None, make_copyright)

# And we can generate the .deb file by calling dpkg-deb
env.Command(debpkg, DEBCONTROLFILE,
	"fakeroot dpkg-deb -b %s %s" % (os.path.abspath(DEBPATH), "$TARGET"))

